package com.ehotting.edsta.circuitDesign.exception;


import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.ehotting.eaf.core.text.UUID;
import com.ehotting.eaf.core.utils.StringUtils;
import com.google.gson.Gson;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.aspectj.lang.reflect.MethodSignature;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import java.lang.reflect.Method;
import java.util.Arrays;


@Aspect
@Component
public class LoggerAspect {

    private LoggerAspect(){}

    @Pointcut("@annotation(com.ehotting.edsta.circuitDesign.exception.LoggerAnnotation)")
    public void logAspectPoint(){}

    /**
     * 环绕
     */
    @Around("logAspectPoint()")
    public Object doAround(ProceedingJoinPoint proceedingJoinPoint) throws Throwable{

        LoggerTar.recLog.info("======================Start======================");
        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
        JoinPoint joinPoint = proceedingJoinPoint;
        //获取注解的描述信息
        String methodDescription=getAspectLogDescription(joinPoint);
        //唯一标识
        String onlyIdentity = request.getHeader("serial");
        if(StringUtils.isEmpty(onlyIdentity)){
            onlyIdentity = UUID.getUUID();
        }
        //请求 url
        String requestURL = request.getRequestURL().toString();
        LoggerTar.recLog.info("serial :{}", onlyIdentity);
        //打印描述信息
        LoggerTar.recLog.info("Description :{}", methodDescription);
        //打印请求 url
        LoggerTar.recLog.info("URL :{}",requestURL);
        LoggerTar.recLog.info("Class Method :{}.{}", joinPoint.getSignature().getDeclaringTypeName(),joinPoint.getSignature().getName());
        //打印请求入参
        String args = Arrays.toString(joinPoint.getArgs());
        LoggerTar.recLog.info("Request Args :{}", args);
        LoggerTar.recLog.info("======================end======================");

        long startTime = System.currentTimeMillis();/**记录一下调用接口的开始时间*/
        /**执行切点，执行切点后，会去依次调用 @Before -> 接口逻辑代码 -> @After -> @AfterReturning*/
        Object result = proceedingJoinPoint.proceed();


        LoggerTar.retLog.info("======================Start======================");
        //唯一标识
        LoggerTar.retLog.info("serial :{}", onlyIdentity);
        //打印请求 url
        LoggerTar.retLog.info("URL :{}",requestURL);
        //打印返回参数
        LoggerTar.retLog.info("Response Args:{}", JSON.toJSONString(result));
        //执行耗时
        LoggerTar.retLog.info("Time-Consuming:{}ms",System.currentTimeMillis()-startTime);
        LoggerTar.retLog.info("======================end======================");
        return result;/**返回接口返参结果*/
    }


    private static String getAspectLogDescription(JoinPoint joinPoint){
        // 获取注解中的参数值
        MethodSignature methodSignature = (MethodSignature)joinPoint.getSignature();
        Method method = methodSignature.getMethod();
        // 获取注解
        LoggerAnnotation annotation = method.getAnnotation(LoggerAnnotation.class);
        // 获取注解Action的value参数的值
        String description = annotation.description();
        return description;
    }


}

